package com.smartsofa.main;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.smartsofa.R;
import com.smartsofa.util.MyUtils;

public class ChangePassword extends Activity {
	
	private EditText et_original_password;
	private EditText et_new_password;
	private EditText et_confirm_password;
	private Button submit_change_password;
	private Button bt_back;
	
	private String old_password;
	private String new_password;
	private String confirm_password;
	private String url;
	private Boolean flag=false;
	private String return_msg;
	//定义网络连接超时判定变量
	private boolean network_timeout=false;
	
	protected void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);			
		setContentView(R.layout.change_password);
		initView();
	}
	
	private void initView(){
		et_original_password=(EditText) findViewById(R.id.et_original_password);
		et_new_password=(EditText) findViewById(R.id.et_new_password);
		et_confirm_password=(EditText) findViewById(R.id.et_confirm_password);
		bt_back=(Button) findViewById(R.id.back);
		bt_back.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				ChangePassword.this.finish();
			}
		});
		submit_change_password=(Button) findViewById(R.id.submit_change_password);
		submit_change_password.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				old_password=et_original_password.getText().toString();
				new_password=et_new_password.getText().toString();
				confirm_password=et_confirm_password.getText().toString();
				if(!confirm_password.equals(new_password)){
					Toast.makeText(ChangePassword.this, "两次输入密码不一致！", Toast.LENGTH_SHORT).show();
				}
				else{
					Thread change_password_thread=new Thread(new change_password());
					change_password_thread.start();
				}
			}
		});
	}
	
	class change_password implements Runnable{

		@Override
		public void run() {
			// TODO Auto-generated method stub
			try {	
				url=MyUtils.getUrl()+"user_updatePassword?userid="+MyUtils.getUSERID()+"&oldpassword="+old_password+"&newpassword="+new_password;
				Log.i("url", url);
				HttpGet request = new HttpGet(url);																
		       // 发送请求
			   BasicHttpParams httpParams = new BasicHttpParams();
			   HttpConnectionParams.setConnectionTimeout(httpParams,3000);
		       HttpClient httpClient = new DefaultHttpClient(httpParams);
		       HttpResponse httpResponse = httpClient.execute(request);					       
		       	
		      if (httpResponse.getStatusLine().getStatusCode() == 200) {
			    	network_timeout=true;
			        String jsonString = EntityUtils.toString(httpResponse.getEntity());
			        Log.i("jsonString", jsonString);
			        JSONObject json = new JSONObject(jsonString);
			        flag=(Boolean) json.get("success");	
			        return_msg=json.getString("msg");
		      }
		      }catch (Exception e) {
		        	 e.printStackTrace();
		      }
			Message msg = content_handler.obtainMessage();
			if(network_timeout){
				if (flag) {				
					msg.what = 1;									
				}else{
					msg.what = 2;			
				}
			}			
			else{
				msg.what = 3;						
			}
			content_handler.sendMessage(msg);
			}
		}
	Handler content_handler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {		
			case 1:
				
				SharedPreferences sp=ChangePassword.this.getSharedPreferences("userInformation", 0);
			    sp.edit().clear().commit();
			    Toast.makeText(ChangePassword.this, "修改密码成功，请重新登录！", Toast.LENGTH_SHORT).show();
				Intent intent=new Intent().setClass(ChangePassword.this, LoginActivity.class);
				startActivity(intent);			
				break;
			case 2:
				Toast.makeText(ChangePassword.this, return_msg, Toast.LENGTH_SHORT).show();
				break;			
			case 3:
				Toast.makeText(ChangePassword.this, "网络连接超时！", Toast.LENGTH_SHORT).show();
				break;
			}
		}
	};

}
